﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.BLL;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public enum SearchSystemLogType
    {
        Time,
        Employee,
    }
    public partial class frmSearchSystemLog : DevComponents.DotNetBar.OfficeForm
    {
        private SearchSystemLogType Type;
        public DataTable SearchResult { get; set; }
        public frmSearchSystemLog(SearchSystemLogType type)
        {
            InitializeComponent();
            Type = type;
            switch (Type)
            {
                case SearchSystemLogType.Time:
                    dtiTime.Visible = true;
                    cbEmployee.Visible = false;
                    Text = "Tìm kiếm theo thời gian.";
                    break;
                case SearchSystemLogType.Employee:
                    dtiTime.Visible = false;
                    cbEmployee.Visible = true;
                    Text = "Tìm kiếm theo nhân viên.";
                    break;
            }
        }

        private void frmSearchSystemLog_Load(object sender, EventArgs e)
        {
            switch (Type)
            {
                case SearchSystemLogType.Time:
                    dtiTime.Value = DateTime.Now;
                    break;
                case SearchSystemLogType.Employee:
                    EmployeeManager manager = new EmployeeManager();
                    DataTable employees = manager.GetAll();
                    if (employees.Rows.Count == 0)
                    {
                        SearchResult = null;
                        Close();
                    }
                    cbEmployee.DataSource = employees;
                    cbEmployee.ValueMember= "EmployeeID";
                    cbEmployee.DisplayMember = "EmployeeName";
                    break;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SystemLogManager manager = new SystemLogManager();
            switch (Type)
            {
                case SearchSystemLogType.Time:
                    DateTime date = dtiTime.Value;
                    SearchResult = manager.SearchByTime(date);
                    break;
                case SearchSystemLogType.Employee:
                    string id = DataUtil.ToString(cbEmployee.SelectedValue);
                    SearchResult = manager.SearchByEmployeeID(id);
                    break;
            }
            if (SearchResult.Rows.Count == 0)
            {
                SearchResult = null;
                MessageBox.Show("Không tìm thấy nhật ký nào phù hợp", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SearchResult = null;
            Close();
        }
    }
}